<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入股票名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="监控状态" prop="monitorStatus">
        <el-select v-model="queryParams.monitorStatus" placeholder="请选择监控状态" clearable>
          <el-option
            v-for="dict in dict.type.monitor_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="持有状态" prop="ownStatus">
        <el-select v-model="queryParams.ownStatus" placeholder="请选择持有状态" clearable>
          <el-option
            v-for="dict in dict.type.own_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bussiness:stock:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bussiness:stock:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bussiness:stock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bussiness:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <!--股票信息信息合并列-->
      <el-table-column label="股票信息" align="center">
        <template slot-scope="scope">
          <div style="text-align: center;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>股票:{{ scope.row.stockName }}<span style="color: red;">|</span>{{ scope.row.stockCode }}</div>
              <div>
                <span v-if="scope.row.monitorType === '已经持有'">
                  <span style="color: red;">持仓</span>:<span style="color: red;">|</span>账:{{ scope.row.stockAccount }}<span style="color: red;">|</span>成:{{ scope.row.costPrice }}<span style="color: red;">|</span>量:{{ scope.row.num }}
                </span>
                <span v-else> {{ scope.row.monitorType }},观察中,暂未持仓 </span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 监控阈值 -->
      <el-table-column label="价格监控" align="center">
        <template slot-scope="scope">
          <div style="text-align: center;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>单日:{{ scope.row.monitorDayPct }}<span style="color: red;">|</span>累计:{{ scope.row.monitorCumulativePct }}</div>
              <div>低价:{{ scope.row.monitorMinPrice }}<span style="color: red;">|</span>高价:{{ scope.row.monitorMaxPrice }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 实时信息 -->
      <el-table-column label="实时数据" align="center">
        <template slot-scope="scope">
          <div style="text-align: center;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>实时涨跌:{{ scope.row.changePct }}</div>
              <div>实时价格:{{ scope.row.currentPrice }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 收益数据 -->
      <el-table-column label="收益统计" align="center">
        <template slot-scope="scope">
          <div style="text-align: center;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>当日收益:
                <span v-if="scope.row.realEarnings > 0" style="color: green;">赚:</span><span v-else style="color: red;">亏:</span>
                {{ scope.row.realEarnings }}
              </div>
              <div>累计收益:
                <span v-if="scope.row.cumulativeEarnings > 0" style="color: green;">赚:</span><span v-else style="color: red;">亏:</span>
                {{ scope.row.cumulativeEarnings }}
              </div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="监|持状态" align="center">
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div><dict-tag :options="dict.type.monitor_status" :value="scope.row.monitorStatus"/></div>
            <div><dict-tag :options="dict.type.own_status" :value="scope.row.ownStatus"/></div>
          </div>
        </template>
      </el-table-column>

      <el-table-column width="400px">
        <template slot="header">
          <span style="padding-left: 100px;">附属信息</span>
        </template>
        <template slot-scope="scope">
          <div style="text-align: left; line-height: 1.5;">
            <div>
              <div><span style="color: green;">均价系统:</span>5日均价: {{scope.row.avgPrice5}}<span style="color: red;">|</span>10日均价{{ scope.row.avgPrice10 }}<span style="color: red;">|</span>30日均价{{ scope.row.avgPrice30 }}</div>
              <div><span style="color: #1890ff;">监控系统:</span>{{ scope.row.remark }}</div>
              <div><span style="color: red;">消息系统:</span>{{ scope.row.notifyMsg }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <!-- 时间信息 -->
      <el-table-column label="时间信息" align="center">
        <template slot-scope="scope">
          <div style="text-align: center;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div>添加:{{ parseTime(scope.row.addTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
              <div>更新:{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bussiness:stock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bussiness:stock:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股票监控对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="股票名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入股票名称" />
        </el-form-item>
        <el-form-item label="股票编码" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入股票编码" />
        </el-form-item>
        <el-form-item label="持有账号" prop="stockAccount">
          <el-input v-model="form.stockAccount" placeholder="请输入持有账号" />
        </el-form-item>
        <el-form-item label="持有数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入持有数量" />
        </el-form-item>
        <el-form-item label="成本价" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="日涨幅监控" prop="monitorDayPct">
          <el-input v-model="form.monitorDayPct" placeholder="请输入日涨幅监控" />
        </el-form-item>
        <el-form-item label="累计涨跌幅监控" prop="monitorCumulativePct">
          <el-input v-model="form.monitorCumulativePct" placeholder="请输入累计涨跌幅监控" />
        </el-form-item>
        <el-form-item label="最低价监控" prop="monitorMinPrice">
          <el-input v-model="form.monitorMinPrice" placeholder="请输入最低价监控" />
        </el-form-item>
        <el-form-item label="最高价监控" prop="monitorMaxPrice">
          <el-input v-model="form.monitorMaxPrice" placeholder="请输入最高价监控" />
        </el-form-item>
        <el-form-item label="监控状态" prop="monitorStatus">
          <el-select v-model="form.monitorStatus" placeholder="请选择监控状态">
            <el-option
              v-for="dict in dict.type.monitor_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="持有状态" prop="ownStatus">
          <el-select v-model="form.ownStatus" placeholder="请选择持有状态">
            <el-option
              v-for="dict in dict.type.own_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="通知信息" prop="notifyMsg">
          <el-input v-model="form.notifyMsg" placeholder="请输入通知信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock } from "@/api/bussiness/stock"

export default {
  name: "Stock",
  dicts: ['own_status', 'monitor_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票监控表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockName: null,
        monitorStatus: null,
        ownStatus: null,
        addTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询股票监控列表 */
    getList() {
      this.loading = true
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockName: null,
        stockCode: null,
        stockAccount: null,
        num: null,
        costPrice: null,
        monitorType: null,
        monitorDayPct: null,
        monitorCumulativePct: null,
        monitorMinPrice: null,
        monitorMaxPrice: null,
        changePct: null,
        currentPrice: null,
        avgPrice5: null,
        avgPrice10: null,
        avgPrice30: null,
        realEarnings: null,
        cumulativeEarnings: null,
        monitorStatus: null,
        ownStatus: null,
        remark: null,
        notifyMsg: null,
        updateTime: null,
        addTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加股票监控"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getStock(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改股票监控"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除股票监控编号为"' + ids + '"的数据项？').then(function() {
        return delStock(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bussiness/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
